$(function () {
	var divheight=$(window).height();//初始化修改div的高度
	divheight=divheight-119;
	$("div.panel-body.hfit").css({height:divheight});
    loadPage(1);
});

//序号
function getNumber(e){
	return e.rowindex+1;
}

//渲染安全生产许可证
function formatSafety(e){
	var value=e.value;
	var row=e.row;
	if(value == 2){
		//return "有   注：有效期至"+row.formatCfeIncludeSafetyValidDate;
		return "<span class='memoDesc' title='"+'有效期至'+row.formatCfeIncludeSafetyValidDate+"'>有</span>";
	}else{
		return "无";
	}
}
//渲染建造师
function formatArchitect(e){
	var value=e.value;
	var row=e.row;
	if(value == 2){
		//return "有   注："+row.cfeIncludeArchitectDesc;
		return "<span class='memoDesc' title='"+row.cfeIncludeArchitectDesc+"'>有</span>";
	}else{
		return "无";
	}
}

//渲染建造师
function formatBuliding(e){
	var value=e.value;
	var row=e.row;
	if(value == 2){
		//return "有   注："+row.cfeIncludeBulidingProjectDesc;
		return "<span class='memoDesc' title='"+row.cfeIncludeBulidingProjectDesc+"'>有</span>";
	}else{
		return "无";
	}
}

//渲染操作按钮
function operateFormat(e){
	var value=e.value;
	var row=e.row;
	var html = new Array();
	var state = row.certificateCompany.cgoods.cfePublishState;
	var comName = row.certificateCompany.cfeComName;
	var showDesc = "";
	if(state == 2 ){
		showDesc = "上架";
	}else{
		showDesc = "下架"
	}
	html.push('<button type="button" class="btn btn-default btn-sm" onclick="confirmAlterState(\''+state+'\',\''+row.cfeGdNum+'\',\''+comName+'\')">'+showDesc+'</button>');
	html.push('<button type="button" class="btn btn-default btn-sm" onclick="alterCertificate(\''+row.cfeId+'\',\''+row.cfeCompanyId+'\')">修改</button>');
	return html.join(" ");
}

//渲染发布状态 
function stateFormat(e){
	var row=e.row;
	var state = row.certificateCompany.cgoods.cfePublishState;
	if(state == 2 ){
		return "已下架"
	}else{
		return "已上架";
	}
}
//渲染公司名称
function getComName(e){
	var row=e.row;
	return row.certificateCompany.cfeComName;
}

//状态修改
function confirmAlterState(currentState,cfeGdNum,comName){
	if(currentState == '' || cfeGdNum == ''){
		layer.alert("未选择资质");
		return ;
	}
	//设置相反状态
	var stateDesc = "";
	if(currentState == 2){
		stateDesc ="上架";
	}else{
		stateDesc = "下架";
	}
	layer.confirm("此操作会 "+stateDesc+" "+comName+"下的所有资质，是否"+stateDesc,{icon: 3, title:stateDesc+'确认'},function(index){
		alterState(currentState,cfeGdNum);
	});
}

//修改发布状态
function alterState(currentState,cfeGdNum){
	
	//设置相反状态
	if(currentState == 2){
		currentState =1;
	}else{
		currentState = 2;
	}
	
	$.ajax({
		url:rootpath+"/certificate/updateState.shtml",
		data:{cfeGdNum:cfeGdNum,
			cfePublishState:currentState,
		},
		type:"post",
		dataType:"json",
		success:function(data){
			if(data.status){
				layer.alert(data.info,function(index){
					window.location.href=rootpath+"/certificate/list.shtml";
				});
			}else{
				layer.alert(data.info);
			}
		},
		error:function(data){
			layer.alert("系统未返回");
		}
	});
	
}

//修改
function alterCertificate(cfeId,comId){
	window.location.href=rootpath+"/certificate/edit.shtml?id="+cfeId+"&comId="+comId;
}

//查询按钮
function query(){
	loadPage(1);
}

function restQuery(){
	$("#queryForm")[0].reset();
	query();
}

function loadTable(pageNum){
	//var paramsData = $("#queryForm").getFormData();
	var paramsData = getFormData($("#queryForm").serializeArray(),pageNum);
	$.ajax({
		url:rootpath+"/certificate/loadByPage.shtml",
		data:paramsData,
		type:"post",
		success:function(data){
			if(!data.status){
				return ;
			}
			var rows=data.data.items;
			var rowsCount = data.data.rowsCount;
			$("#certificate_table_hl").table(rows);
		},
		error:function(){
		}
		
	});
}

function loadPage(pageNum){
	var paramsData = getFormData($("#queryForm").serializeArray(),pageNum);
	$.ajax({
		url:rootpath+"/certificate/loadByPage.shtml",
		data:paramsData,
		type:"post",
		success:function(data){
			if(!data.status){
				$("#certificate_table_hl").table("");
				//初始化分页插件
				$("#tablePaging").html("");
				$("#tablePaging").tablePaging(1,10,0,"loadTable");
				return ;
			}
			var rows=data.data.items;
			var rowsCount = data.data.rowsCount;
			$("#certificate_table_hl").table(rows);
			//初始化分页插件
			$("#tablePaging").html("");
			$("#tablePaging").tablePaging(1,10,rowsCount,"loadTable");
		},
		error:function(){
			
		}
		
	});
}

//格式化表单数据
function getFormData(formSerializeArray,pageNum){
	var obj = {};
	var isPush = false;
	$.each(formSerializeArray,function(){
		if(this.value == null || this.value==""){
			return 0;
		}
		obj[this.name] = this.value;
		isPush =true;
	});
	var offSet=(pageNum-1)*10;
	obj.pageOffset = offSet;
	obj.pageSize = 10;
	return obj;
}

function isConfirmDelete(){
	layer.confirm("此操作会无法撤销，是否确认删除",{icon: 3,title:'删除确认'},function(index){
		layer.close(index);
		deleteCer();
	});
}

//删除资质
function deleteCer(){
	var ids = getSelect();
	var comIds = getSelectRowComIds();
	if(ids.length<=0){layer.msg("请选择一行数据！", {icon: 5 });return;}
	$.ajax({
		url:rootpath+"/certificate/delete.shtml",
		data:{
			cfeIds:ids.join(","),
			cfecomIds:comIds.join(",")
		},
		type:"post",
		success:function(data){
			window.location.href=rootpath+"/certificate/list.shtml";
		},
		error:function(data){
			layer.alert("系统未返回");
		}
	});
	
}

//获取选中行的ID
function getSelect(){
	var rows=$("#certificate_table_hl").getTableSelects();
	var ids=new Array();
	 $.each(rows,function(i,obj){
		ids[i]=obj.cfeId;
	});
	return ids;
}

//得到选中行的公司ID列表
function getSelectRowComIds(){
	var rows=$("#certificate_table_hl").getTableSelects();
	var comIds=new Array();
	 $.each(rows,function(index,obj){
		for(var i = 0 , j = comIds.length;i<j;i++){
			var thisId = comIds[i];
			if(thisId == obj.cfeCompanyId){
				comIds.splice(i,1);
			}
		}
		comIds.push(obj.cfeCompanyId);
	});
	 return comIds;
}